<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Clients_2 extends CI_Controller {

    public function index()
    {
        $this->load->model('Clients_model');
        $ruc = null;
        $name = null;
        $address = null;
        $telephone = null;
        $email = null;
      
        if($this->input->post('ruc')){

            $ruc =$this->input->post('ruc');
        }
        if($this->input->post('name')){
            $name = $this->input->post('name');
        }
        if($this->input->post('address')){
            $address = $this->input->post('address');
        }
        if($this->input->post('telephone')){
            $telephone = $this->input->post('telephone');
        }
         if($this->input->post('email')){
            $email= $this->input->post('email');
        }
         
        $clients = $this->Clients_model->getAll();
        if ($ruc != null || $name != null || $address != null || $telephone != null || $email != null) {
	        $clients = $this->Clients_model->search($ruc, $name, $address, $telephone, $email);
	    }
                
       
        $vars['ruc'] = $ruc;
		$vars['name'] = $name;
		$vars['address'] = $address;
        $vars['telephone'] = $telephone;
        $vars['email'] = $email;
       
        
        
        $vars['content_view']='clients_list_index';
        $vars['clients'] = $clients;
        $vars['menu']='Client';
        $this->load->view('template', $vars);
         
    }
    public function shop_by_client() {
        $this->load->model('Sales_model');
        $this->load->model('Clients_model');
        $id_client = null;
        $desde = null;
        $hasta = null;

        if ($this->input->post('client')) {
            $id_client = $this->input->post('client');
        }
        if ($this->input->post('desde')) {
            $desde = $this->input->post('desde');
            $datei = $this->input->post('desde');
            $date = str_replace('/', '-', $datei);
            $datedesde = date("Y-m-d", strtotime($date) );
        }
        if ($this->input->post('hasta')) {
            $hasta = $this->input->post('hasta');
            $datei = $this->input->post('hasta');
            $date = str_replace('/', '-', $datei);
            $datehasta = date("Y-m-d", strtotime($date) );
        }


        $vars['clients'] = $this->Clients_model->getAll();
        if ($id_client != null || $desde != null || $hasta != null) {
            $shops = $this->Sales_model->shopbyclient($id_client, $datedesde, $datehasta);
        } else {
            $shops = array();
        }     

        $vars['shops'] = $shops;
        $vars['desde'] = $desde;
        $vars['hasta'] = $hasta;
        $vars['client'] = $id_client;
        $vars['content_view'] = 'shopbyclient_form';
        $vars['menu'] = 'Client';
        $this->load->view('template', $vars);
    }
	
    public function add($success_message = null)
    {
       // ***** /
                
	   if (isset($success_message)){
		$vars['success_message'] = true;
	   }
                                
       $vars['content_view'] = 'client_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'Client'; //
	   $this->load->view('template', $vars);
            
       //*********/
    }
    
   
     public function edit($code)
     {
        // ***** /
        $this->load->model('Clients_model');
           
       $vars['client'] = $this->Clients_model->find($code);
           
       $vars['content_view'] = 'client_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'Client'; //
	   $this->load->view('template', $vars);
       
            
       /*********/
    }
    
    
    public function submit()
    {
         if( $this->input->post('client_id') != ""){
             
             $this->load->model('Clients_model');
              
             $id = $this->input->post('client_id');
            
			 $vars['name'] = $this->input->post('name');
			 $vars['ruc'] = $this->input->post('ruc');
			 $vars['address'] = $this->input->post('address');
			   
			 $vars['telephone'] = $this->input->post('telephone');
			 $vars['email'] = $this->input->post('email');
             
             $vars['edit'] = $this->Clients_model->update($id, $vars);
             
             $vars['client'] = $this->Clients_model->find($id);
             $vars['content_view'] = 'client_form'; // varibale cque contiene el nombre del formulario  llamar
             $vars['menu'] = 'Client'; //
             $this->load->view('template', $vars);
            
             
            }else{
                
                
             $this->load->model('Clients_model');
                
             $data= array();
               
	        $data['name'] = $this->input->post('name');
	        $data['ruc'] = $this->input->post('ruc');
	        $data['address'] = $this->input->post('address');
	       
	        $data['telephone'] = $this->input->post('telephone');
	        $data['email'] = $this->input->post('email');
	        $data['status'] = 1;
	        $data['create'] = date('Y-m-d H:i:s');
                
                
                
                
                $vars['success'] = $this->Clients_model->add($data); //despues de agregar
                
         
                $vars['content_view'] = 'client_form'; // varibale cque contiene el nombre del formulario  llamar
                $vars['menu'] = 'Client'; //
                $this->load->view('template', $vars);
//                $this->index();
                 
            }
         
     
    }

    public function delete($id)
    {
        $this->load->model('Clients_model');
        $this->Clients_model->delete($id);
        $this->index();
       
      
       
    }
    
}